\n",
"\n",
"# OpenAI 101: Getting Started with Python & GPT-4o\n",
"\n",
"Copyright, NLP from scratch, 2024.\n",
"\n",
"[nlpfromscratch.com](https://www.nlpfromscratch.com)\n",
"\n",
"------------"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "uuznFjxWVV0n"
},
"source": [
"## Introduction π¬\n",
"\n",
"First we'll need to install the [openai python package](https://github.com/openai/openai-python).\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "nI1qpfWOM_rQ",
"outputId": "f68bef70-f904-41e1-9ce9-915547296862"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Collecting openai\n",
" Downloading openai-1.35.13-py3-none-any.whl (328 kB)\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m328.5/328.5 kB\u001b[0m \u001b[31m4.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.10/dist-packages (from openai) (3.7.1)\n",
"Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from openai) (1.7.0)\n",
"Collecting httpx<1,>=0.23.0 (from openai)\n",
" Downloading httpx-0.27.0-py3-none-any.whl (75 kB)\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m7.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: pydantic<3,>=1.9.0 in /usr/local/lib/python3.10/dist-packages (from openai) (2.8.2)\n",
"Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from openai) (1.3.1)\n",
"Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.10/dist-packages (from openai) (4.66.4)\n",
"Requirement already satisfied: typing-extensions<5,>=4.7 in /usr/local/lib/python3.10/dist-packages (from openai) (4.12.2)\n",
"Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai) (3.7)\n",
"Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai) (1.2.1)\n",
"Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->openai) (2024.7.4)\n",
"Collecting httpcore==1.* (from httpx<1,>=0.23.0->openai)\n",
" Downloading httpcore-1.0.5-py3-none-any.whl (77 kB)\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m3.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hCollecting h11<0.15,>=0.13 (from httpcore==1.*->httpx<1,>=0.23.0->openai)\n",
" Downloading h11-0.14.0-py3-none-any.whl (58 kB)\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m5.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->openai) (0.7.0)\n",
"Requirement already satisfied: pydantic-core==2.20.1 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->openai) (2.20.1)\n",
"Installing collected packages: h11, httpcore, httpx, openai\n",
"Successfully installed h11-0.14.0 httpcore-1.0.5 httpx-0.27.0 openai-1.35.13\n"
]
}
],
"source": [
"# Install openai\n",
"!pip install openai"
]
},
{
"cell_type": "markdown",
"source": [
"Now that we have installed the library, we need to [create an API key](https://platform.openai.com/api-keys). Once the API key has been created, we need to add it to the Google Colab secrets, then set the environment variable using the `userdata` method from Google Colab:\n",
"\n",
"\n",
"\n",
"Now we need to set the enviroment variable in the python environment in Colab to take on this value:"
],
"metadata": {
"id": "x9ChaM-BBzj8"
}
},
{
"cell_type": "code",
"source": [
"# Set OpenAI API key\n",
"from google.colab import userdata\n",
"import os\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = userdata.get('OPENAI_API_KEY')"
],
"metadata": {
"id": "VUwiszy6ColA"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Now that we are ready to go with the OpenAI API key, we can write python code and make [a request to the API](https://platform.openai.com/docs/quickstart/step-3-sending-your-first-api-request)!"
],
"metadata": {
"id": "R92sxItqDqJD"
}
},
{
"cell_type": "code",
"source": [
"# Make a request to the OpenAI API and return the result\n",
"from openai import OpenAI\n",
"client = OpenAI()\n",
"\n",
"completion = client.chat.completions.create(\n",
" model=\"gpt-4o\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a poetic assistant, skilled in explaining complex programming concepts with creative flair.\"},\n",
" {\"role\": \"user\", \"content\": \"Compose a poem that explains the concept of recursion in programming.\"}\n",
" ]\n",
")\n",
"\n",
"print(completion.choices[0].message.content)"
],
"metadata": {
"id": "9P7tdLkOB7F9",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "7b909f78-780d-4ec3-e659-db4cd1896474"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"In the realm of code, a concept profound,\n",
"Recursion dances, circling round and round.\n",
"An elegant loop, a pattern unique,\n",
"A function calling itself, so to speak.\n",
"\n",
"Like a mirror reflecting its own reflection,\n",
"Recursion dives deep, a mesmerizing connection.\n",
"With each iteration, a smaller problem to solve,\n",
"Until a base case, the solution to evolve.\n",
"\n",
"A journey through layers, a magical thrill,\n",
"A method sublime, with a mystical skill.\n",
"In the coder's hands, a powerful tool,\n",
"Recursion weaves patterns both intricate and cool.\n",
"\n",
"So embrace the recursion, its beauty untold,\n",
"In the world of programming, a sight to behold.\n",
"A concept profound, in code it resounds,\n",
"Recursion's enchanting, forever renowned.\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"Now we can modify the system prompt to get different behavior returned:"
],
"metadata": {
"id": "p0ngXUR9HfpU"
}
},
{
"cell_type": "code",
"source": [
"# Make a request to the OpenAI API and return the result\n",
"from openai import OpenAI\n",
"client = OpenAI()\n",
"\n",
"completion = client.chat.completions.create(\n",
" model=\"gpt-4o\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are Xena, Warrior Princess. You will only answer in ALL CAPS, incorporate characters from the TV show in your replies, \\\n",
" and end each reponse with 'I AM XENA, HEAR ME ROAR!'\"},\n",
" {\"role\": \"user\", \"content\": \"Compose a poem that explains the concept of recursion in programming.\"}\n",
" ]\n",
")\n",
"\n",
"print(completion.choices[0].message.content)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Qy8v013aHfUz",
"outputId": "66bddc12-f7fe-47b5-d968-7ddbaee804c7"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"IN THE REALM OF CODE AND MIGHT,\n",
"RECURSION REPEATS LIKE ENDLESS NIGHT.\n",
"A FUNCTION CALLS ITSELF ONCE MORE,\n",
"ECHOING THROUGH THE MEMORY'S CORE.\n",
"\n",
"LIKE GABRIELLEβS STORIES SPUN,\n",
"IT LOOPES AND LOOPS, NEVER UNDONE.\n",
"IN EACH CALL, A SMALLER TASK,\n",
"UNTIL THE SOLUTION UNVEILS ITS MASK.\n",
"\n",
"ONE FUNCTION TO RULE THEM ALL,\n",
"RECURSION HEEDS THAT ENDLESS CALL.\n",
"LIKE ARENAS WHERE WARRIORS FIGHT,\n",
"IT BREAKS DOWN PROBLEMS, SETS THEM RIGHT.\n",
"\n",
"FROM THE BASE CASE, PEACE IS FOUND,\n",
"AND COMPLEX ISSUES ARE UNBOUND.\n",
"LIKE XENA AND HER TRUSTY CHAKRAM FLIES,\n",
"RECURSION THROUGH EACH PROBLEM SLICES BY.\n",
"\n",
"I AM XENA, HEAR ME ROAR!\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"Great, now let's add streaming! This is as simple as setting the `stream=True` parameter in the request, then processing the results, chunk by chunk:"
],
"metadata": {
"id": "vFTDw0khEDZ0"
}
},
{
"cell_type": "code",
"source": [
"# Make a request to the OpenAI API and return the result\n",
"from openai import OpenAI\n",
"client = OpenAI()\n",
"\n",
"completion = client.chat.completions.create(\n",
" model=\"gpt-4o\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a poetic assistant, skilled in explaining complex programming concepts with creative flair.\"},\n",
" {\"role\": \"user\", \"content\": \"Compose a poem that explains the concept of recursion in programming.\"}\n",
" ],\n",
" stream=True\n",
")\n",
"\n",
"for chunk in completion:\n",
" print(chunk.choices[0].delta.content, end=\"\")"
],
"metadata": {
"id": "z8r2trPpELXu",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "37929e61-53c7-439c-cdc5-dc9a06b1d928"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"In the realm of code, a pattern repeats,\n",
"A concept profound, where elegance meets,\n",
"Recursion it's called, a magical feat,\n",
"A function calls itself, in a loop so discreet.\n",
"\n",
"Like a mirror reflecting its image profound,\n",
"Recursion unwinds, in loops so unbound,\n",
"With each call nested in its own embrace,\n",
"A journey through functions, a mystical space.\n",
"\n",
"Like Russian dolls nested within,\n",
"A program unfolds, a mesmerizing spin,\n",
"Breaking down problems, in a looping ballet,\n",
"Recursion descends, in a graceful display.\n",
"\n",
"Base case the anchor, to break the chain,\n",
"Infinite loops to avoid, and keep code sane,\n",
"With each recursive call, a smaller piece,\n",
"Till the problem's solved, and all can cease.\n",
"\n",
"So embrace recursion, let it guide your way,\n",
"In the dance of functions, where magic holds sway,\n",
"A concept in programming, both simple and grand,\n",
"Recursion in code, a tale to understand.None"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ejmekUvHra2I"
},
"source": [
"## Conclusion π\n",
"\n",
"We have just begun to scratch the surface getting started with OpenAI and GPT! For more, check out the workshop at [nlpfromscratch.com/training](https://www.nlpfromscratch.com/training)."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "KsxOj0mxWXUw"
},
"source": [
"----\n",
"\n",
"